var mongoose = require('./setmongoose');

// 用户数据模型
var jjkSchema = new mongoose.Schema({
    name:String,
    age:Number,
    sex:String,
    phone:String,
    email:String,
    password:String,
    createdAt:Date,
    updatedAt:Date,
})  

// 检查模型是否已经存在，避免重复定义
var jjkModel;
try {
    jjkModel = mongoose.model('jjk');
} catch (error) {
    jjkModel = mongoose.model('jjk', jjkSchema, 'jjk');
}

// 我的接单数据模型
var myOrderSchema = new mongoose.Schema({
    orderId: {
        type: String,
        required: true,
        unique: true
    },
    userId: {
        type: String,
        required: true  // 用户ID，用于区分不同用户的订单
    },
    title: {
        type: String,
        required: true
    },
    pickup: {
        type: String,
        required: true
    },
    delivery: {
        type: String,
        required: true
    },
    price: {
        type: String,
        required: true
    },
    status: {
        type: String,
        enum: ['ongoing', 'completed', 'cancelled'],
        default: 'ongoing'
    },
    // 服务类型字段
    serviceType: {
        type: String,
        enum: ['express', 'takeout'],  // express: 快递代取, takeout: 帮拿外卖
        required: true
    },
    acceptTime: {
        type: Date,
        default: Date.now
    },
    completeTime: {
        type: Date
    },
    // 原始订单信息
    originalOrder: {
        type: mongoose.Schema.Types.Mixed,
        default: {}
    }
}, {
    timestamps: true  // 自动添加createdAt和updatedAt
});

// 快递代取订单模型
var expressOrderSchema = new mongoose.Schema({
    orderId: {
        type: String,
        required: true,
        unique: true
    },
    userId: {
        type: String,
        required: true
    },
    // 配送信息
    deliveryLocation: {
        type: String,
        required: true
    },
    deliveryTime: {
        type: String,
        required: true
    },
    // 费用信息
    baseFee: {
        type: Number,
        required: true,
        default: 5
    },
    tipAmount: {
        type: Number,
        default: 0
    },
    totalPrice: {
        type: Number,
        required: true
    },
    // 更多要求
    hasMoreRequirements: {
        type: Boolean,
        default: false
    },
    specialNote: {
        type: String,
        default: ''
    },
    // 订单状态
    status: {
        type: String,
        enum: ['pending', 'accepted', 'picking', 'delivering', 'completed', 'cancelled'],
        default: 'pending'
    },
    // 时间信息
    orderTime: {
        type: Date,
        default: Date.now
    },
    acceptTime: {
        type: Date
    },
    completeTime: {
        type: Date
    }
}, {
    timestamps: true
});

// 帮拿外卖订单模型
var takeoutOrderSchema = new mongoose.Schema({
    orderId: {
        type: String,
        required: true,
        unique: true
    },
    userId: {
        type: String,
        required: true
    },
    // 服务信息
    selectedTime: {
        type: String,
        required: true
    },
    orderDetail: {
        type: String,
        required: true
    },
    // 费用信息
    basePrice: {
        type: Number,
        required: true,
        default: 3
    },
    tipAmount: {
        type: Number,
        default: 3
    },
    totalAmount: {
        type: Number,
        required: true
    },
    // 订单状态
    status: {
        type: String,
        enum: ['pending', 'accepted', 'purchasing', 'delivering', 'completed', 'cancelled'],
        default: 'pending'
    },
    // 时间信息
    orderTime: {
        type: Date,
        default: Date.now
    },
    acceptTime: {
        type: Date
    },
    completeTime: {
        type: Date
    }
}, {
    timestamps: true
});

var MyOrderModel = mongoose.model('MyOrder', myOrderSchema, 'myOrders');
var ExpressOrderModel = mongoose.model('ExpressOrder', expressOrderSchema, 'expressOrders');
var TakeoutOrderModel = mongoose.model('TakeoutOrder', takeoutOrderSchema, 'takeoutOrders');

module.exports = {
    jjkModel,
    MyOrderModel,
    ExpressOrderModel,
    TakeoutOrderModel
};